package com.android.task4;

// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
public final class MapTile extends Object {
	public static final int LATITUDE_INDEX = 0;
	public static final int LONGITUDE_INDEX = 1;

	// if changed, change as well comment for constructor
	protected static final String DELIM = ".";

	protected Point mPoint;
	protected int mZoomLevel;

	public MapTile(final Point point, final int zoomLevel) {
		this.mPoint = point;
		this.mZoomLevel = zoomLevel;
	}

	public MapTile(final int coords[], final int zoomLevel) {
		this.mPoint = new Point(coords[0], coords[1]);
		this.mZoomLevel = zoomLevel;
	}

	public Point getPoint() {
		return this.mPoint;
	}

	public int getZoomLevel() {
		return this.mZoomLevel;
	}

	@Override
	public String toString() {
		return "MapTile[x=" + this.mPoint.getX() + ";y=" + this.mPoint.getY()
				+ ";zoomLevel=" + this.mZoomLevel + "]";
	}

	/***
	 * Creates MapTile from resourceName.
	 * 
	 * @param resourceName
	 *            resource name, format: ([0-9]+.){2}[0-9]+.
	 */
	public MapTile(final String resourceName) {
		String[] tokens = resourceName.split("\\" + DELIM);
		if (tokens.length == 3) {
			this.mZoomLevel = Integer.parseInt(tokens[0]);
			this.mPoint = new Point(Integer.parseInt(tokens[2]), Integer
					.parseInt(tokens[1]));
		} else
			throw new IllegalArgumentException(
					"Invalid format provided for MapTile.");
	}

	public String getResourceName() {
		return MapTile.getResourceName(this.mPoint, this.mZoomLevel,
				MapTile.DELIM);
	}

	public String getTileUri() {
		return getResourceName(this.mPoint, this.mZoomLevel, "/");
	}

	public static MapTile createFromResourceName(final String resourceName) {
		return new MapTile(resourceName);
	}

	protected static String getResourceName(final Point point,
			final int zoomLevel, final String delim) {
		return zoomLevel + delim + point.getX() + delim + point.getY();
	}

	public void show() {
		System.out.println(this.toString());
	}
}
